﻿using EMoney.IStock.Client.Business;
using EMoney.IStock.Client.Business.Cpyx;
using EMoney.IStock.Client.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class CPYXController : Controller
    {
        // 操盘英雄
        // GET: /CPYX/

        /// <summary>
        /// 英雄榜
        /// </summary>
        /// <returns></returns>
        public ActionResult yxb()
        {
            int userid = 0;
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(Request["token"]);
            }
            else
            {
                TokenBll.RemoveUserIDCookie();
                //userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }

            ViewBag.UserID = userid;

            //距离榜单重置时间
            DateTime dtNow = DateTime.Now;

            CpyxBll cpyxBll = new CpyxBll();

            ViewBag.ResetTips = cpyxBll.GetResetTime(dtNow);

            return View();
        }

        /// <summary>
        /// 我的记录
        /// </summary>
        /// <returns></returns>
        public ActionResult wdjl()
        {
            int userid = 0;
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(Request["token"]);
            }
            else
            {
                TokenBll.RemoveUserIDCookie();
                //userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }
            
            ViewBag.UserID = userid;
            
            return View();
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <returns></returns>
        public ActionResult help()
        {
            return View();
        }

        /// <summary>
        /// 获取我的记录
        /// </summary>
        /// <returns></returns>
        public string result()
        {
            string reqFlag = Request["flag"];

            if (string.IsNullOrEmpty(reqFlag) && reqFlag != "operate" && reqFlag != "record")
            {
                return string.Empty;
            }

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            int type = 0;

            if (reqFlag.Equals("record", StringComparison.OrdinalIgnoreCase))
            {
                type = 1;
            }
            
            CpyxBll cpyxBll = new CpyxBll();

            return cpyxBll.GetUserResult(userid,type);
        }

        /// <summary>
        /// 获取排行榜
        /// </summary>
        /// <returns></returns>
        public string armory()
        {
            int userid = 0;
            string reqWeek = Request["week"];
            string reqUserID = Request["uid"];

            if (string.IsNullOrEmpty(reqWeek) || string.IsNullOrEmpty(reqUserID))
            {
                return string.Empty;
            }

            if (reqWeek != "cw" && reqWeek != "pw" && reqWeek != "ppw")
            {
                return string.Empty;
            }

            DateTime dtNow = DateTime.Now;
            switch (reqWeek)
            {
                case "pw":
                    if (dtNow < Convert.ToDateTime("2014-8-25"))
                    {
                        return string.Empty;
                    }
                    break;
                case "ppw":
                    if (dtNow < Convert.ToDateTime("2014-9-1"))
                    {
                        return string.Empty;
                    }
                    break;
                default:
                    break;
            }

            if (!int.TryParse(reqUserID, out userid))
            {
                return string.Empty;
            }

            CpyxBll cpyxBll = new CpyxBll();

            return cpyxBll.GetArmory(userid, reqWeek);
        }

        /// <summary>
        /// 获取关闭提示Cookie
        /// </summary>
        /// <returns></returns>
        public string gt()
        {
            int userid = 0;
            string ret = string.Empty;
            
            string reqUserID = Request["uid"];

            if (!string.IsNullOrEmpty(reqUserID) && int.TryParse(reqUserID, out userid))
            {
                ret = CookieHelper.GetCookie(string.Concat("cpyx-yxb-tips-", userid), string.Concat("cpyx-yxb-tips-", userid));
            }

            return ret;
        }

        /// <summary>
        /// 关闭提示信息记录Cookie
        /// </summary>
        public void ct()
        {
            int userid = 0;
            
            string reqUserID = Request["uid"];

            if (!string.IsNullOrEmpty(reqUserID) && int.TryParse(reqUserID, out userid))
            {
                CookieHelper.SetCookie(string.Concat("cpyx-yxb-tips-", userid), string.Concat("cpyx-yxb-tips-", userid), "1", 720);
            }
        }
    }
}
